<template>
  <div class="category">
    <el-table
      :data="tableData"
      style="width: 100%; margin-bottom: 20px;"
      row-key="id"
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
    >
      <el-table-column prop="name" label="类别名称" width="600" />
      <el-table-column prop="described" label="描述" width="700" />
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            type="primary"
            size="small"
            icon="el-icon-edit"
            @click="editData(scope.$index, scope.row)"
          />
          <el-button
            type="danger"
            size="small"
            icon="el-icon-delete"
            @click="delData(scope.$index, scope.row)"
          />
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :current-page="page.page"
      :page-sizes="[2, 4, 6, 8]"
      :page-size="page.pagesize"
      background
      layout="total, sizes, prev, pager, next, jumper"
      :total="page.total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
    <el-dialog title="提示" :visible.sync="dialogVisible" width="30%" @close="btnCancel">
      <span slot="default">
        <el-form ref="editForm" :rules="rules" :model="editForm" label-width="80px">
          <el-form-item prop="name" label="类别名称">
            <el-input v-model="editForm.name" />
          </el-form-item>
          <el-form-item prop="described" label="描述">
            <el-input v-model="editForm.described" />
          </el-form-item>
        </el-form>
      </span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="onSubmit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getCategory, getCategoryPage, delCategory, editCategory } from '@/api/setting';

export default {
  name: 'Category',
  data() {
    return {
      dialogVisible: false,
      editForm: {
        name: '',
        described: '',
      },
      page: {
        page: 1,
        pagesize: 2,
        total: 0,
      },
      tableData: [],
      currentPage: 1,
      rules: {
        name: [
          { required: true, message: '请输入行业名称', trigger: 'blur' },
          { min: 1, max: 8, message: '文字请控制在1~8字以内', trigger: 'blur' },
        ],
        described: [
          { required: true, message: '请输入描述内容', trigger: 'blur' },
          { min: 1, max: 20, message: '描述内容请控制在1~20字以内', trigger: 'blur' },
        ],
      },
    };
  },
  created() {
    this.getCategory();
    this.getCategoryPage();
  },
  methods: {
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`);
      // 每页条数动态绑定
      this.page.pagesize = val;
      this.getCategoryPage();
    },
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`);
      // 当前页动态绑定
      this.page.page = val;
      this.getCategoryPage();
    },
    async getCategory() {
      const { data } = await getCategory();
      // console.log(data);
      this.page.total = data.length;
    },
    async getCategoryPage() {
      const { data } = await getCategoryPage({
        _page: this.page.page,
        _limit: this.page.pagesize,
      });
      // console.log(data);
      this.tableData = data;
    },
    delData(index, row) {
      this.$confirm('是否删除该数据？删除之后不可恢复！', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          // console.log(row);
          return delCategory(row.id);
        })
        .then(() => {
          this.getCategory();
          this.getCategoryPage();
          this.$message.success('删除成功');
        })
        .catch(() => {
          this.$message.warning('删除失败');
        });
    },
    editData(index, row) {
      this.editForm = row;
      console.log(row);
      this.dialogVisible = true;
    },
    onSubmit() {
      this.$refs.editForm.validate(async(valid) => {
        if (valid) {
          await editCategory(this.editForm, this.editForm.id).then((res) => {
            // console.log(res);
            if (res.status === 200) {
              this.$message.success('编辑成功');
              this.dialogVisible = false;
            } else {
              this.$message.warning('编辑失败');
            }
          });
        } else {
          return false;
        }
      });
    },
    btnCancel() {
      this.dialogVisible = false;
      this.getCategoryPage();
    },
  },
};
</script>

<style lang="scss" scoped>
.category {
  .el-table {
    margin-top: 20px;
  }
  .el-pagination {
    float: right;
    overflow: hidden;
    margin-top: 20px;
  }
}
</style>
